package com.th.supcom.pds.outdrugreturnapply.client.widget;

import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.widget.MedicareCardField;

/**
 * 退药主面板.
 * 
 * @author yzhang
 */

public class OutDrugReturnApplyMainPanel extends ContentPanel
{
    private MedicareCardField m_medicareCardField;
    private OutDrugReturnApplyButtons m_applyButtons;
    private OutDrugReturnApplyFields m_applyFields;
    private OutDrugReturnApplyAction m_action;
    private OutDrugReturnApplyMasterGridPanel m_masterGridPanel;
    private OutDrugReturnApplyDetailGridPanel m_detailGridPanel;

    public OutDrugReturnApplyMainPanel ()
    {
        initUI ();
        initAction ();
        initListeners ();
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        // 设置样式
        setHeaderVisible (false);
        setBorders (false);
        setLayout (new BorderLayout ());
        addMedicareCard ();
        // 初始化对象
        m_applyButtons = new OutDrugReturnApplyButtons ();
        m_applyFields = new OutDrugReturnApplyFields (m_medicareCardField);
        OutDrugReturnApplyQueryPanel queryPanel = new OutDrugReturnApplyQueryPanel (m_applyFields, m_applyButtons);
        OutDrugReturnPatientPanel patientPanel = new OutDrugReturnPatientPanel (m_applyFields);
        m_detailGridPanel = new OutDrugReturnApplyDetailGridPanel ();
        m_masterGridPanel = new OutDrugReturnApplyMasterGridPanel ();

        // 设置布局和大小
        BorderLayoutData northData = new BorderLayoutData (LayoutRegion.NORTH, 150);
        northData.setMargins (new Margins (4, 4, 4, 4));
        BorderLayoutData centerData = new BorderLayoutData (LayoutRegion.CENTER);
        centerData.setMargins (new Margins (4, 4, 4, 4));
        BorderLayoutData westData = new BorderLayoutData (LayoutRegion.WEST, 280);
        westData.setMargins (new Margins (4, 0, 4, 4));
        LayoutContainer northPanelContainer = new LayoutContainer ();
        northPanelContainer.setLayout (new RowLayout (Orientation.VERTICAL));
        northPanelContainer.add (queryPanel, new RowData (1, 0.4, new Margins (0)));
        northPanelContainer.add (patientPanel, new RowData (1, 0.6, new Margins (0)));

        add (northPanelContainer, northData);
        add (m_detailGridPanel, centerData);
        add (m_masterGridPanel, westData);
        ContentPanel bottomContainer = new ContentPanel ();
        bottomContainer.setHeaderVisible (false);
        bottomContainer.setButtonAlign (HorizontalAlignment.CENTER);
        bottomContainer.addButton (m_applyButtons.getComfirmBtn ());
        bottomContainer.addButton (m_applyButtons.getClearBtn ());
        setBottomComponent (bottomContainer);
        m_medicareCardField.focus ();
    }

    /**
     * 初始化事件
     */
    private void initAction ()
    {
        m_action = new OutDrugReturnApplyAction ();
        m_action.setFields (m_applyFields);
        m_action.setDetailGridPanel (m_detailGridPanel);
        m_action.setMasterGridPanel (m_masterGridPanel);
        m_action.setMainPanel (this);
    }

    /**
     * 初始化事件监听
     */
    private void initListeners ()
    {
        OutDrugReturnApplyListener listener = new OutDrugReturnApplyListener ();
        listener.setAction (m_action);
        listener.setButtons (m_applyButtons);
        listener.setFields (m_applyFields);
        listener.setMasterGridPanel (m_masterGridPanel);
        listener.setKeyNavComponent (this);
        listener.initListeners ();
    }

    /**
     * 添加刷卡组件
     */
    private void addMedicareCard ()
    {
        m_medicareCardField = new MedicareCardField (this)
        {
            @Override
            public void handleEventAfter ()
            {
                PatientInfoModel pati = m_medicareCardField.getModelValue ();
                if (pati == null)
                {
                    ClientUtils.alertMessageBox ("通过卡号找不到患者信息，请联系计算机中心！", null);
                    return;
                }
                Map <String, Object> conditionMap = new HashMap <String, Object> ();
                conditionMap.put ("patientId", pati.getPatiId ());
                conditionMap.put ("prescNo", m_applyFields.getPrescNoField ().getValue ());
                conditionMap.put ("beginDate", m_applyFields.getBeginField ().getValue ());
                conditionMap.put ("endDate", m_applyFields.getEndField ().getValue ());
                m_action.findDrugReturnInfo (conditionMap);
                return;
            }
        };
    }

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}